package com.jike.jkgame.data;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.jike.jkgame.Utilis;
import com.jike.jkgame.db.ResourcesDAO;

public class Equipment {
	public int id;
	public byte type;
	public String name;
	public String descript;
	public byte star_level;
	public int attack_min;
	public int attack_max;
	public int defense_min;
	public int defense_max;
	public byte energy_recover_min;
	public byte energy_recover_max;
	public byte ammo_recover_min;
	public byte ammo_recover_max;
	public byte exp_addon_min;
	public byte exp_addon_max;
	public byte money_addon_min;
	public byte money_addon_max;
	
	private static Equipment getEquipment(DataInputStream dis, byte[] buff) {
		Equipment equip = new Equipment();
		
		
		try {
			equip.id = dis.readInt();
			equip.type = dis.readByte();
			equip.name = Utilis.getString(dis, buff);
			equip.descript = Utilis.getString(dis, buff);
			equip.star_level = dis.readByte();
			equip.attack_min = dis.readInt();
			equip.attack_max = dis.readInt();
			equip.defense_min = dis.readInt();
			equip.defense_max = dis.readInt();
			equip.energy_recover_min = dis.readByte();
			equip.energy_recover_max = dis.readByte();
			equip.ammo_recover_min = dis.readByte();
			equip.ammo_recover_max = dis.readByte();
			equip.exp_addon_min = dis.readByte();
			equip.exp_addon_max = dis.readByte();
			equip.money_addon_max = dis.readByte();
			equip.money_addon_max = dis.readByte();
		} catch (IOException e) {
			return null;
		}
		
		return equip;
	}
	
	public static int parseDataFile(InputStream in) {
		
		DataInputStream dis = new DataInputStream(in);
		int count = 0;
		ResourcesDAO dao = ResourcesDAO.get();
		
		byte[] buff = new byte[4096];
		try {
			int type = dis.readInt();
			
//			if (type != FILE_TYPE) {
//				return 0;
//			}
			int myVersion = dao.getDataFileVersion(type);
			int version = dis.readInt();
			
			if (myVersion >= version) {
				return 0;
			}
			dao.clearEquipments();
			
			Equipment equip = null;
			
			do {
				equip = getEquipment(dis, buff);
				dao.insertEquipment(equip);
				count++;
			}while(equip != null);
			dao.updateDataFileVersion(type, version);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	
}
